package com.seattleweb.gdocdump;

import org.gnome.gdk.Event;
import org.gnome.gtk.Button;
import org.gnome.gtk.Entry;
import org.gnome.gtk.ErrorMessageDialog;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Image;
import org.gnome.gtk.Justification;
import org.gnome.gtk.Label;
import org.gnome.gtk.Menu;
import org.gnome.gtk.MenuBar;
import org.gnome.gtk.MenuItem;
import org.gnome.gtk.ProgressBar;
import org.gnome.gtk.Table;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;

public class GDocDump {


	ProgressBar progressBar = new ProgressBar();
	Entry entryPassword = new Entry();
	
	Entry entryUsername = new Entry();

	public static void main(String[] args) {
		Gtk.init(args);

		GDocDump gdocdump = new GDocDump();
		gdocdump.init();
	}

	public void init() {
		final Window w;
		final VBox x;
		final Button b;

		w = new Window();

		x = new VBox(false, 3);

		MenuBar menuBar = new MenuBar();
		MenuItem fileItem = new MenuItem("File");
		menuBar.append(fileItem);

		Menu quitMenu = new Menu();
		MenuItem quitItem = new MenuItem("Quit");

		quitItem.connect(new MenuItem.Activate() {

			public void onActivate(MenuItem menuItem) {
				Gtk.mainQuit();
			}
		});

		quitMenu.append(quitItem);
		fileItem.setSubmenu(quitMenu);

		x.packStart(menuBar, false, false, 3);

		Image image = new Image();

		image.setImage("logo-med.png");

		x.add(image);

		x.add(new Label("Google account info:"));

		//entryPassword.setInvisibleChar('*');
		
		entryPassword.setVisibleChars(false);
		
		Table table = new Table(2, 2, false);

		Label labelUsername = new Label("Username: ");
		Label labelPassword = new Label("Password: ");

		labelUsername.setJustify(Justification.RIGHT);
		labelPassword.setJustify(Justification.RIGHT);
		
		labelUsername.setAlignment(1, labelUsername.getAlignmentY());
		labelPassword.setAlignment(1, labelPassword.getAlignmentY());
		
		table.attach(labelUsername,0,1,0,1);
		table.attach(labelPassword,0,1,1,2);
			
		table.attach(entryUsername,1,2,0,1);
		table.attach(entryPassword,1,2,1,2);
				
		x.add(table);
		
		x.add(progressBar);

		b = new Button("Dump");
		// b.setSensitive(false);
		
		x.add(b);

		b.connect(new Button.Clicked() {
			public void onClicked(Button source) {

				try {
					Dumper dumper = new Dumper(progressBar);
					dumper.authenticate(entryUsername.getText(), entryPassword
							.getText());

					b.setSensitive(false);
					dumper.start();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

					ErrorMessageDialog dialog = new ErrorMessageDialog(w,
							"GDocs login failed.",
							"Check credentials and try again.");

					dialog.run();
					dialog.hide();
				}
			}
		});

		w.add(x);
		w.setTitle("gdocdump");
		w.showAll();

		w.connect(new Window.DeleteEvent() {
			public boolean onDeleteEvent(Widget source, Event event) {
				Gtk.mainQuit();
				return false;
			}
		});

		Gtk.main();
	}

}
